package com.googlecode.lyang.cxfstudy.compress;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.http.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.http.gzip.GZIPOutInterceptor;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class BigDataServiceTest {

    private static final String address = "http://localhost:9000/ws/compress/bigDataService";
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
        factoryBean.getInInterceptors().add(new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
        factoryBean.getInInterceptors().add(new GZIPInInterceptor());
        factoryBean.getOutInterceptors().add(new GZIPOutInterceptor());
        
        factoryBean.setServiceClass(BigDataServiceImpl.class);
        factoryBean.setAddress(address);
        factoryBean.create();
    }

    @Test
    public void testGetBigData() {
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setAddress(address);
        factoryBean.setServiceClass(BigDataService.class);
        Object obj = factoryBean.create();
        
        Client client = ClientProxy.getClient(obj);
        Endpoint endpoint = client.getEndpoint();
        endpoint.getInInterceptors().add(new GZIPInInterceptor());
        endpoint.getOutInterceptors().add(new GZIPOutInterceptor());
        
        BigDataService service = (BigDataService) obj;
        Assert.assertNotNull(service);
        
        String name = "my big data";
        int size = 1024 * 1024 * 5;
        
        long start = System.currentTimeMillis();
        BigData bigData = service.getBigData(name, size);
        long stop = System.currentTimeMillis();
        System.out.println("Time: " + (stop - start));
        
        Assert.assertNotNull(bigData);
        Assert.assertEquals(name, bigData.getName());
        Assert.assertEquals(size, bigData.getData().length());
    }
}